<template>
  <el-dialog title="选择供应商"
             v-if="showFlag"
             :visible.sync="showFlag"
             :modal= false
             width="80%"
             center
  >
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="供应商名称" label-width="100" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入供应商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商代码" label-width="100" prop="supplierCode">
        <el-input
          v-model="queryParams.supplierCode"
          placeholder="请输入供应商代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商电话" label-width="100" prop="supplierPhone">
        <el-input
          v-model="queryParams.supplierPhone"
          placeholder="请输入供应商电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="supplierList" @selection-change="handleSelectionChange" @current-change="handleCurrent">
      <el-table-column  width="55" align="center" >
        <template v-slot="scope">
          <el-radio v-model="selectedItemId" :label="scope.row.id" @change="handleRowChange(scope.row)">{{""}}</el-radio>
        </template>
      </el-table-column>
      <el-table-column label="公司代码" align="center" width="80" prop="factoryCode"/>
      <el-table-column label="公司名称" align="center" width="80" prop="factoryName"/>
      <el-table-column label="供应商类别" align="center" prop="supplierCategory"/>
      <el-table-column label="供应商代码" align="center" prop="supplierCode" width="120">
        <template slot-scope="scope">
          <el-button
            type="text"
          >{{ scope.row.supplierCode }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="供应商名称" align="center" prop="supplierName"/>
      <el-table-column label="供应商税号" align="center" prop="supplierEin"/>
      <el-table-column label="供应商地址" align="center" prop="address" show-overflow-tooltip/>
      <el-table-column label="供应商电话" align="center" prop="supplierPhone"/>
      <el-table-column label="供应商开户行" align="center" prop="supplierBank"/>
      <el-table-column label="供应商账号" align="center" prop="supplierAcc"/>
      <el-table-column label="供应商联系人" align="center" prop="supplierContact"/>
      <el-table-column label="供应商联系人电话" align="center" prop="supplierContactNumber"/>
      <el-table-column label="运输方式" align="center" prop="tsWays"/>
      <el-table-column label="运输类别" align="center" prop="tsType"/>
      <el-table-column label="供应商运输距离（KM）" align="center" prop="supplierTsKm"/>
      <el-table-column label="供应商运输时间（小时）" align="center" prop="supplierTsTime"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="showFlag=false">取 消</el-button>
    </div>
  </div>
  </el-dialog>
</template>

<script>
import {listSupplier, getSupplier, delSupplier, addSupplier, updateSupplier} from "@/api/supplychain/supplier";
import {getListClass} from "@/api/base/class";


export default {
  name: "Supplier",
  data() {
    return {
      showFlag:false,
      selectedItemId: undefined,
      selectedRows: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      classList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商管理表格数据
      supplierList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        factoryCode: null,
        factoryName: null,
        materialCategory: null,
        itemChildNo: null,
        materialNo: null,
        supplierCategory: null,
        materialName: null,
        supplierName: null,
        supplierCode: null,
        address: null,
        supplierPhone: null,
        supplierEin: null,
        supplierBank: null,
        supplierAcc: null,
        proCycle: null,
        supplierTsKm: null,
        supplierTsTime: null,
        deliveryCycle: null,
        deliveryFe: null,
        adjustRate: null,
        adjustDate: null,
        orderNumMonth: null,
        finishNumMonth: null,
        nofinishRateMonth: null,
        packageCapacity: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        factoryCode: [
          { required: true, message: "工程代码不能为空", trigger: "blur" }
        ],
        supplierCode: [
          { required: true, message: "供应商代码不能为空", trigger: "blur" }
        ],
        supplierName: [
          { required: true, message: "供应商名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    show() {
      this.showFlag = true;
      this.selectedRows={}
      this.selectedItemId=undefined
    },
    /** 查询供应商管理列表 */
    getList() {
      this.loading = true;
      listSupplier(this.queryParams).then(response => {
        this.supplierList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        factoryCode: null,
        factoryName: null,
        materialCategory: null,
        itemChildNo: null,
        materialNo: null,
        supplierCategory: null,
        materialName: null,
        supplierName: null,
        supplierCode: null,
        address: null,
        supplierPhone: null,
        supplierEin: null,
        supplierBank: null,
        supplierAcc: null,
        proCycle: null,
        supplierTsKm: null,
        supplierTsTime: null,
        deliveryCycle: null,
        deliveryFe: null,
        adjustRate: null,
        adjustDate: null,
        orderNumMonth: null,
        finishNumMonth: null,
        nofinishRateMonth: null,
        packageCapacity: null,
        supplierContact: null,
        supplierContactNumber: null,
        tsWays: null,
        tsType: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      getListClass().then(resp => {
        this.classList = resp.data;
        this.reset();
        this.open = true;
        this.title = "添加供应商管理";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSupplier(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改供应商管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSupplier(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSupplier(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除供应商管理编号为"' + ids + '"的数据项？').then(function () {
        return delSupplier(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('supplychain/supplier/export', {
        ...this.queryParams
      }, `supplier_${new Date().getTime()}.xlsx`)
    },
    handleCurrent(row){
      if(row){
        this.selectedRows = row;
      }
    },
    handleRowDbClick(row){
      if(row){
        this.selectedRows = row;
        this.$emit('onSelected',this.selectedRows);
        this.showFlag = false;
      }
    },
    // 单选选中数据
    handleRowChange(row) {
      if(row){
        this.selectedRows = row;
      }
    },
    /** 提交按钮 */
    confirmSelect() {
      if(this.selectedItemId ==null || this.selectedItemId==0){
        this.$notify({
          title:'提示',
          type:'warning',
          message: '请至少选择一条数据!'
        });
        return;
      }
      this.$emit('onSelected',this.selectedRows);
      this.showFlag = false;
    },
  }
};
</script>
